package com.chenix.aicodehelper.Ai;

import com.chenix.aicodehelper.Tools.MSNWeatherTool;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.service.AiServices;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AiCoderHelperServiceFactory {
    // 模型
    @Resource
    private ChatModel qwenChatModel;
    // 流式模型
    @Resource
    private StreamingChatModel qwenStreamingChatModel;
    // 内容检索器
    @Resource
    private ContentRetriever contentRetriever;


    @Bean
    public AiCoderHelperService create() {
        //会话记忆最大记忆的个数
        ChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);
        AiCoderHelperService aiCoderHelperService = AiServices.builder(AiCoderHelperService.class)
                .chatMemory(chatMemory)//会话记忆
                .streamingChatModel(qwenStreamingChatModel)
                .chatMemoryProvider(message -> MessageWindowChatMemory.withMaxMessages(10))//会话记忆提供者
                /*.systemMessageProvider((Function<MessageContext, String>) context -> {
                    // 从上下文获取memoryId（多会话场景）
                    String memoryId = context.memoryId();
                    // 根据不同memoryId返回不同系统提示
                    if ("admin".equals(memoryId)) {
                        return "你是管理员专用助手，可执行高级操作。";
                    } else {
                        return "你是普通用户助手，提供基础帮助。";
                    }
                })*/
                .chatModel(qwenChatModel)
                .contentRetriever(contentRetriever)//RAG内容检索器
                .tools(new MSNWeatherTool())
                .build();
        return aiCoderHelperService;
    }
}
